﻿# -*- coding:utf-8 -*-
"""
author:wangxing
email: wangxing dot pku at gmail dot com
"""
import urllib

appID = ""


def cn2en(thestr):
    froml = "zh-cn"
    tol = "en"
    uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId="+appID
    uri +="&text=" +  urllib.quote(thestr) + "&from=" + froml +"&to=" + tol
    print uri
    result = urllib.urlopen(uri).read()
    #result = ""
    return result

def en2cn(thestr):
    tol = "zh-cn"
    froml = "en"
    uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId="+appID
    uri +="&text=" +  urllib.quote(thestr) + "&from=" + froml +"&to=" + tol
    print uri
    result = urllib.urlopen(uri).read()
    #result = ""
    return result

if __name__ == "__main__":
    cnsentence = u"不再幻想到东京疯狂 shopping"
    ensentence = "test translation"
    #print cn2en(cnsentence.encode("utf-8"))
    tmpstr = en2cn(ensentence)
    print tmpstr.decode("utf-8").encode("gbk")
